home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 53 / PC Actual CD 53.iso / Share / Progra / python / BeOpen-Python-2.0.exe / TEST_LARGEFILE.PY < prev    next >
Encoding:
Text File  |  2000-09-28  |  3.5 KB  |  130 lines

  1. #!python
  2.  
  3. #----------------------------------------------------------------------
  4. # test largefile support on system where this makes sense
  5. #
  6. #XXX how to only run this when support is there
  7. #XXX how to only optionally run this, it will take along time
  8. #----------------------------------------------------------------------
  9.  
  10. import test_support
  11. import os, struct, stat, sys
  12.  
  13.  
  14. # only run if the current system support large files
  15. f = open(test_support.TESTFN, 'w')
  16. try:
  17.     # 2**31 == 2147483648
  18.     f.seek(2147483649L)
  19. except OverflowError:
  20.     raise test_support.TestSkipped, "platform does not have largefile support"
  21. else:
  22.     f.close()
  23.  
  24.  
  25. # create >2GB file (2GB = 2147483648 bytes)
  26. size = 2500000000L
  27. name = test_support.TESTFN
  28.  
  29.  
  30. # on Windows this test comsumes large resources:
  31. #  it takes a long time to build the >2GB file and takes >2GB of disk space
  32. # therefore test_support.use_large_resources must be defined to run this test
  33. if sys.platform[:3] == 'win' and not test_support.use_large_resources:
  34.     raise test_support.TestSkipped, \
  35.         "test requires %s bytes and a long time to run" % str(size)
  36.  
  37.  
  38.  
  39. def expect(got_this, expect_this):
  40.     if test_support.verbose:
  41.         print '%s =?= %s ...' % (`got_this`, `expect_this`),
  42.     if got_this != expect_this:
  43.         if test_support.verbose:
  44.             print 'no'
  45.         raise test_support.TestFailed, 'got %s, but expected %s' %\
  46.             (str(got_this), str(expect_this))
  47.     else:
  48.         if test_support.verbose:
  49.             print 'yes'
  50.  
  51.  
  52. # test that each file function works as expected for a large (i.e. >2GB, do
  53. # we have to check >4GB) files
  54.  
  55. if test_support.verbose:
  56.     print 'create large file via seek (may be sparse file) ...'
  57. f = open(name, 'w')
  58. f.seek(size)
  59. f.write('a')
  60. f.flush()
  61. expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
  62. if test_support.verbose:
  63.     print 'check file size with os.fstat'
  64. f.close()
  65. if test_support.verbose:
  66.     print 'check file size with os.stat'
  67. expect(os.stat(name)[stat.ST_SIZE], size+1)
  68.  
  69. if test_support.verbose:
  70.     print 'play around with seek() and read() with the built largefile'
  71. f = open(name, 'r')
  72. expect(f.tell(), 0)
  73. expect(f.read(1), '\000')
  74. expect(f.tell(), 1)
  75. f.seek(0)
  76. expect(f.tell(), 0)
  77. f.seek(0, 0)
  78. expect(f.tell(), 0)
  79. f.seek(42)
  80. expect(f.tell(), 42)
  81. f.seek(42, 0)
  82. expect(f.tell(), 42)
  83. f.seek(42, 1)
  84. expect(f.tell(), 84)
  85. f.seek(0, 1)
  86. expect(f.tell(), 84)
  87. f.seek(0, 2) # seek from the end
  88. expect(f.tell(), size + 1 + 0)
  89. f.seek(-10, 2)
  90. expect(f.tell(), size + 1 - 10)
  91. f.seek(-size-1, 2)
  92. expect(f.tell(), 0)
  93. f.seek(size)
  94. expect(f.tell(), size)
  95. expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
  96. f.close()
  97.  
  98. if test_support.verbose:
  99.     print 'play around with os.lseek() with the built largefile'
  100. f = open(name, 'r')
  101. expect(os.lseek(f.fileno(), 0, 0), 0)
  102. expect(os.lseek(f.fileno(), 42, 0), 42)
  103. expect(os.lseek(f.fileno(), 42, 1), 84)
  104. expect(os.lseek(f.fileno(), 0, 1), 84)
  105. expect(os.lseek(f.fileno(), 0, 2), size+1+0)
  106. expect(os.lseek(f.fileno(), -10, 2), size+1-10)
  107. expect(os.lseek(f.fileno(), -size-1, 2), 0)
  108. expect(os.lseek(f.fileno(), size, 0), size)
  109. expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
  110. f.close()
  111.  
  112.  
  113. # XXX add tests for truncate if it exists
  114. # XXX has truncate ever worked on Windows? specifically on WinNT I get:
  115. #     "IOError: [Errno 13] Permission denied"
  116. ##try:
  117. ##    newsize = size - 10
  118. ##    f.seek(newsize)
  119. ##    f.truncate()
  120. ##    expect(f.tell(), newsize)
  121. ##    newsize = newsize - 1
  122. ##    f.seek(0)
  123. ##    f.truncate(newsize)
  124. ##    expect(f.tell(), newsize)
  125. ##except AttributeError:
  126. ##    pass
  127.  
  128. os.unlink(name)
  129.  
  130.